package com.example.dayu521.sg_oss.conf;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//开启@RabbitListener支持 https://docs.spring.io/spring-amqp/docs/current/reference/html/#async-annotation-driven-enable
@EnableRabbit
public class AmqpCnf implements RabbitListenerConfigurer {

    //https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.spring-mvc.customize-jackson-objectmapper
    //JacksonAutoConfiguration
    //Jackson2ObjectMapperBuilder 配置了ObjectMapper
    @Bean
    public MessageConverter MyMessageConverter(ObjectMapper objectMapper){
        var c=new Jackson2JsonMessageConverter(objectMapper);
        return c;
    }

    //配置RabbitTemplate https://docs.spring.io/spring-boot/docs/current/reference/html/messaging.html#messaging.amqp.sending
    @Bean
    public RabbitTemplate rabbitTemplate(RabbitTemplateConfigurer rabbitTemplateConfigurer, ConnectionFactory connectionFactory){
        var rabbitTemplate = new RabbitTemplate();
        rabbitTemplateConfigurer.configure(rabbitTemplate,connectionFactory);
        return rabbitTemplate;
    }

    //接收消息 https://docs.spring.io/spring-boot/docs/current/reference/html/messaging.html#messaging.amqp.receiving
    //默认配置了SimpleRabbitListenerContainerFactory给@RabbitListener

    //什么是容器 https://docs.spring.io/spring-amqp/docs/current/reference/html/#container
    //两种容器选择 https://docs.spring.io/spring-amqp/docs/current/reference/html/#choose-container
    //他们的配置 https://docs.spring.io/spring-amqp/docs/current/reference/html/#containerAttributes
    //容器工厂,用于@RabbitListener注册容器 https://docs.spring.io/spring-amqp/docs/current/reference/html/#using-container-factories

    //不使用注解,直接配置处理方法
    //他和在方法上标注@RabbitListener效果是一样的
    //但都必须要先开启@EnableRabbit
    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
//        SimpleRabbitListenerEndpoint endpoint = new SimpleRabbitListenerEndpoint();
//        endpoint.setId("123321amqp");
//        endpoint.setQueueNames("anotherQueue");
//        endpoint.setMessageListener(message -> {
//            // processing
//        });
//        registrar.registerEndpoint(endpoint);
    }

    @Bean
    public Queue recover_queue_local(){
        return QueueBuilder.durable("sg_oss.recover.queue.local").build();
    }

    @Bean
    public Queue recover_queue_minio(){
        return QueueBuilder.durable("sg_oss.recover.queue.minio").build();
    }

    @Bean
    public TopicExchange recover_exchange(){
        return ExchangeBuilder.topicExchange("sg_oss.recover.exchange").durable(true).build();
    }

    @Bean
    public Binding recover_binding_local(){
        return BindingBuilder.bind(recover_queue_local()).to(recover_exchange()).with("obj_duplicate.local");
    }

    @Bean
    public Binding recover_binding_minio(){
        return BindingBuilder.bind(recover_queue_minio()).to(recover_exchange()).with("obj_duplicate.minio");
    }

    /*******************/
    public static final String order_exehange = "sg.order.exchange";
    @Bean
    public DirectExchange order_exchange(){
        return ExchangeBuilder.directExchange(order_exehange).durable(true).build();
    }

    public static final String order_queue_incrby = "sg_oss.recover.queue.incrby";
    @Bean
    public Queue recover_queue_incrby(){
        return QueueBuilder.durable(order_queue_incrby).build();
    }

    public static final String order_incrby_bind_key = "sg.order.key";
    @Bean
    public Binding recover_binding_incrby(){
        return BindingBuilder.bind(recover_queue_incrby()).to(order_exchange()).with(order_incrby_bind_key);
    }

    public static final String order_queue_incrby_null= "sg_oss.recover.queue.incrby.null";
    @Bean
    public Queue recover_queue_incrby_null(){
        return QueueBuilder.durable(order_queue_incrby_null).build();
    }

    public static final String order_incrby_null_bind_key = "sg.order.key.null";
    @Bean
    public Binding recover_binding_incrby_null(){
        return BindingBuilder.bind(recover_queue_incrby_null()).to(order_exchange()).with(order_incrby_null_bind_key);
    }

}
